// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

define(['../Core', '../Ui/Button', '../Storage', '../Event', '../Mouse'], function (Core, Button, Storage, Event, Mouse) {
    var Modal = function(objModal) {
        this.Background = objModal.Background;
        this.BorderColor = objModal.BorderColor;
        this.BorderWidth = objModal.BorderWidth;
        this.Color = objModal.Color;
        this.ElementType = 'modal';
        this.FontSize = '14px';
        this.FontFamily = 'Arial, sans-serif';
        this.Height = objModal.Height;
        this.Id = Core.GenerateUUID();
        this.Text = objModal.Text;
        this.Width = objModal.Width;
        this.X = objModal.X;
        this.Y = objModal.Y;
    };

    Modal.prototype = {
        Add: function() {
            Core.DrawRectangleWithText(this.Context, this.X, this.Y, this.Width, this.Height, this.Background, this.BorderColor, this.BorderWidth, this.Text, this.X + 10,  this.Y + 10, this.Color, this.FontSize, this.FontFamily, 'left');

            // Add button for modal
            this.Storage.Storage[0].Add();
        },

        Close: function() {
            // Remove both element itself and
            // its buttons
            Core.RemoveDomElementById('modal-' + this.Id);
            Core.RemoveDomElementById('button-' + this.Storage.Storage[0].Id);
        },

        Create: function(objModal) {
            return new Modal(objModal);
        },

        Initialize: function() {
            Core.CreateCanvasForElement(this, 'style', 'background: rgba(0, 0, 0, .3)');

            // Override auto width
            this.Width = this.Width == 'auto' ? Core.MeasureText(this.Text, 'normal', this.FontFamily, this.FontSize).Width + 20 : this.Width;

            // Override auto positioning
            this.X = Core.CenterDimensionToWindow(this.X, this.Width, 'width');
            this.Y = Core.CenterDimensionToWindow(this.Y, this.Height, 'height');

            // New storage
            this.Storage = Storage();

            // Add button for modal
            var modalButton = Button({
                BackgroundColor: 'rgba(255, 255, 255, .9)',
                BorderColor: 'transparent',
                BorderWidth: 0,
                Height: 30,
                Width: 200,
                X: this.X + Core.CenterDimension('auto', this.Width, 200),
                Y: this.Y + 60,
                Label: 'Close',
                LabelColor: '#000',
                LabelFont: 'Verdana, sans-serif',
                LabelSize: '14pt'
            });

            // Attach and initialize click event for closing
            // a modal
            Event('click', modalButton, {
                Action: 'close',
                Data: this
            }).AttachEvent();

            // Initialize button
            // We have to do it before pushing element
            // to Storage cause initialization
            // creates properties, that we want to
            // save
            modalButton.Initialize();

            // Store UI element
            this.Storage.Push(modalButton);

            // Attach click event for modal
            Mouse.AttachClickEvent(this);
        }
    };

    return Modal.prototype.Create;
});